package handler

import (
	"2302a/weekly/api/request"
	"2302a/weekly/api/resp"
	"2302a/weekly/internal/dao"
	"2302a/weekly/internal/model"
	"github.com/gin-gonic/gin"
	"time"
)

func ReleaseComment(c *gin.Context) {
	var req request.ReleaseCommentReq

	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数信息错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	var user model.User
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		resp.Error(c, 10000, "用户不存在", false)
		return
	}
	var video model.Video
	dao.GetOneById(req.VideoId, &video)
	if video.Id == 0 {
		resp.Error(c, 10000, "视频不存在", false)
		return
	}
	parse, _ := time.Parse(time.DateTime, req.CommentTime)

	comment := model.Comment{
		UserId:      userId,
		VideoId:     req.VideoId,
		Content:     req.Content,
		CommentTime: parse,
		Give:        req.Give,
	}
	dao.Create(&comment)
	resp.Success(c, "评论成功", true)
}

func ListComment(c *gin.Context) {
	var req request.ListCommentReq

	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数信息错误", err.Error())
		return
	}

	userId := c.GetUint("userId")
	var user model.User
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		resp.Error(c, 10000, "用户不存在", false)
		return
	}
	var commentList []model.Comment
	dao.GetListPageByFinds(&model.Comment{Id: req.VideoId}, &commentList)
	resp.Success(c, "评论列表成功", commentList)
}
